package com.it_labs.zookeeper;

import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

/**
 * @author Simon
 * Created by Simon on 2019/9/26
 **/
public class ZkclientTest {
    ZkClient zkClient;

    @Before
    public void init() {
        zkClient = new ZkClient("127.0.0.1:2181", 5000, 5000);
    }

    @Test
    public void createPersistent() {
        zkClient.createPersistent("/data", "aaaaa");

        Object o = zkClient.readData("/data");
        System.out.println(o);
    }

    @Test
    public void subscribeDataChanges() throws InterruptedException {
        zkClient.subscribeDataChanges("/data", new IZkDataListener() {
            @Override
            public void handleDataChange(String s, Object o) throws Exception {
                System.out.println(s);
                System.out.println(o);
            }

            @Override
            public void handleDataDeleted(String s) throws Exception {
                System.out.println(s);
            }
        });
        TimeUnit.DAYS.sleep(1L);
    }
}
